package gemini.castor.ui.client.page.widget.phone;

import gemini.castor.ui.client.page.widget.NumberFieldStandard;
import gemini.castor.ui.client.utils.UiConstants;

import java.util.Iterator;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.validation.client.InvalidConstraint;
import com.google.gwt.validation.client.interfaces.IValidator;

public class PhoneWidget extends Composite {

	@UiTemplate("PhoneWidget.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, PhoneWidget> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	private IValidator<PhoneObject> validator;
	private PhoneObject object;

	public void setPhoneObject(PhoneObject object) {
		this.object = object;
	}

	public PhoneWidget() {
		initWidget(binder.createAndBindUi(this));
		validator = GWT.create(PhoneObject.class);
		loadData();
	}

	@UiHandler("chxAsAboveOut")
	void onCheckAsAboveOut(ValueChangeEvent<Boolean> e) {
		tbxPhoneOut.setVisible(!chxAsAboveOut.getValue());
		if(chxAsAboveOut.getValue()){
			chxAsAboveOut.addStyleDependentName("checked");
		}else{
			chxAsAboveOut.removeStyleDependentName("checked");
		}
	}

	@UiField
	NumberFieldStandard tbxPhoneOut;

	@UiField
	NumberFieldStandard tbxPhoneCell;

	@UiField
	CheckBox chxAsAboveIn;

	@UiHandler("chxAsAboveIn")
	void onCheckAsAboveIn(ValueChangeEvent<Boolean> e) {
		tbxPhoneIn.setVisible(!chxAsAboveIn.getValue());
		if(chxAsAboveIn.getValue()){
			chxAsAboveIn.addStyleDependentName("checked");
		}else{
			chxAsAboveIn.removeStyleDependentName("checked");
		}
	}

	@UiField
	NumberFieldStandard tbxPhoneIn;

	@UiField
	CheckBox chxAsAboveOut;

	public boolean isValidate() {
		resetStyle();
		bind();
		boolean result = true;
		Set<InvalidConstraint<PhoneObject>> invalidConstraints = validator
				.validate(object);
		Iterator<InvalidConstraint<PhoneObject>> it = invalidConstraints
				.iterator();

		while (it.hasNext()) {
			if (result) {
				result = false;
			}
			InvalidConstraint<PhoneObject> ic = it.next();
			showError(ic.getItemName(), ic.getMessage());
		}

		return result;
	}

	private void showError(String widgetName, String errorString) {
		if (widgetName.equals("phoneCell")) {
			tbxPhoneCell.addStyleDependentName("error");
		} else if ((widgetName.equals("phoneOut")) && (!chxAsAboveOut.getValue())) {
			tbxPhoneOut.addStyleDependentName("error");
		} else if ((widgetName.equals("phoneIn")) && (!chxAsAboveIn.getValue())) {
			tbxPhoneIn.addStyleDependentName("error");
		}
	}

	public void bind() {
		object.setPhoneCell(tbxPhoneCell.getValue());
		
		if(chxAsAboveOut.getValue()){
			object.setPhoneOut(tbxPhoneCell.getValue());
		}else{
			object.setPhoneOut(tbxPhoneOut.getValue());
		}
		
		if(chxAsAboveIn.getValue()){
			object.setPhoneIn(tbxPhoneCell.getValue());
		}else{
			object.setPhoneIn(tbxPhoneIn.getValue());
		}
	}

	public void reset(){
		resetValue();
		resetStyle();
	}
	
	private void resetValue(){
		tbxPhoneCell.setValue("", true);
		tbxPhoneIn.setValue("", true);
		tbxPhoneOut.setValue("", true);
	}
	
	private void resetStyle(){
		tbxPhoneCell.removeStyleDependentName("error");
		tbxPhoneIn.removeStyleDependentName("error");
		tbxPhoneOut.removeStyleDependentName("error");
	}
	private void loadData() {
		tbxPhoneCell.setMaxLength(UiConstants.MAX_LEN_PHONE);
		tbxPhoneIn.setMaxLength(UiConstants.MAX_LEN_PHONE);
		tbxPhoneOut.setMaxLength(UiConstants.MAX_LEN_PHONE);
		chxAsAboveOut.addStyleDependentName("checked");
		chxAsAboveIn.addStyleDependentName("checked");
	}
}
